﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using MobiMenu.Restaurant.Web.Utility;

namespace MobiMenu.Restaurant.Web
{
    /// <summary>
    ///RequestData 的摘要说明
    /// </summary>
    //[DataContract(Namespace = "MobiMenu.Restaurant.Web")]
    public class RequestData<T>
    {
        public RequestData()
        {
            //QuaryDic = new Dictionary<string, string>();
        }

        public RequestData(NameValueCollection qs)
        {            
            //QuaryDic = new Dictionary<string, string>();
            var paramsField = new ReadOnlyNameValueCollection();
            paramsField.SetReadOnly(false);

            OperationType operation;
            if (RequestHelper.GetOperationType(qs, out operation))
            {
                Operation = operation;
            }
            else
            {
                Operation = OperationType.SelectAll;
            }


            switch (Operation)
            {
                case OperationType.SelectAll:
                case OperationType.Select:
                    InitForSelectAll(qs);
                    break;
                case OperationType.Insert:
                    InitForInsert(qs);
                    break;
                case OperationType.Update:
                    InitForUpdate(qs);
                    break;
                case OperationType.Delete:
                    InitForDelete(qs);
                    break;
            }            

            foreach(string key in qs.AllKeys)
            {
                if (RequestHelper.IsReservedKey(key))
                {
                    continue;
                }

                paramsField.Add(key, qs[key]);
            }

            paramsField.SetReadOnly(true);
            Params = paramsField;
        }

        private void InitForUpdate(NameValueCollection qs)
        {
            throw new NotImplementedException();
        }

        private void InitForInsert(NameValueCollection qs)
        {
            string insertDataStr = qs[RequestHelper.InsertJsonDataKey];
            if (string.IsNullOrEmpty(insertDataStr))
            {
                return;
            }

            try
            {
                InsertObjects = JsonHelper<List<T>>.ReadObject(insertDataStr);
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("InitForInsert", ex);
                throw ex;
            }
        }

        private void InitForDelete(NameValueCollection qs)
        {
            string deleteIdsStr = qs[RequestHelper.DeleteIdsKey];
            if (string.IsNullOrEmpty(deleteIdsStr))
            {
                return;
            }

            int[] deleteIds;
            if (RequestHelper.GetInt32Array(qs, RequestHelper.DeleteIdsKey, out deleteIds))
            {
                DeleteIds = new List<int>();
                DeleteIds.AddRange(deleteIds);
            }
        }

        private void InitForSelectAll(NameValueCollection qs)
        {
            string sortField;
            if (RequestHelper.GetSortField(qs, out sortField))
            {
                bool sortDesc;
                if (RequestHelper.GetSortDesc(qs, out sortDesc))
                {
                    SortByDesc = sortDesc;

                }

                SortField = sortField;
            }

            int pageIndex, pageSize;
            if (RequestHelper.GetPage(qs, out pageIndex, out pageSize))
            {
                PageNo = pageIndex;
                PageSize = pageSize;
            }
        }

        public List<T> InsertObjects
        {
            get;
            private set;
        }

        public OperationType Operation
        {
            get;
            private set;
        }

        //[DataMember(Name = RequestHelper.SortFieldKey)]
        public string SortField
        {
            get;
            private set;
        }

        //[DataMember(Name = RequestHelper.SortByDescKey)]
        public bool? SortByDesc
        {
            get;
            private set;
        }

        //[DataMember(Name = RequestHelper.PageNoKey)]
        public int? PageNo
        {
            get;
            private set;
        }

        //[DataMember(Name = RequestHelper.PageSizeKey)]
        public int? PageSize
        {
            get;
            private set;
        }

        ////[DataMember(Name = RequestHelper.QueryDicKey)]
        //public Dictionary<string, string> QuaryDic
        //{
        //    get;
        //    private set;
        //}

        public List<int> DeleteIds
        { 
            get; 
            private set; 
        }

        private NameValueCollection _params;
        private static object _lockParams = new object();
        public NameValueCollection Params
        {
            get
            {
                if (_params == null)
                {
                    lock (_lockParams)
                    {
                        if (_params == null)
                        {
                            _params = new ReadOnlyNameValueCollection();
                            //foreach (KeyValuePair<string, string> kv in QuaryDic)
                            //{
                            //    _params.Add(kv.Key, kv.Value);
                            //}

                            (_params as ReadOnlyNameValueCollection).SetReadOnly(true);
                        }
                    }
                }

                return _params;
            }
            private set
            {
                _params = value;
            }
        }

        public bool NeedPage()
        {
            return PageSize.HasValue;
        }

        public bool NeedSort()
        {
            return !string.IsNullOrEmpty(SortField);
        }

        //public bool NeedSearch()
        //{
        //    return Params.Count > 0;
        //}

        private class ReadOnlyNameValueCollection : NameValueCollection
        {
            public void SetReadOnly(bool value)
            {
                IsReadOnly = value;
            }
        }
    }
}